package multithreaded;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.BlockingQueue;

import utility.PageInfo;

import net.htmlparser.jericho.Source;

/**
 * Threaded version of the page retriever.
 * Constantly checks the url Blocking Queue for a url to dequeue
 * and retrieve an html page from.
 * After it has returned the html Source it adds it to the page 
 * Blocking Queue for parsing.
 * @author Daniel Anderson and Luc H. Le
 * @version TCSS422 2012
 */
public class ThreadedPageRetriever implements Runnable {
	
	/**Number of pages retrieved so far.*/
	private int my_retrieved_count;
	
	/**Queue of urls to be retrieved.*/
	private BlockingQueue<String> my_url_queue;
	
	/**Queue of pages that have been retrieved.*/
	private BlockingQueue<PageInfo> my_pages;
	
	/**Ends the thread if it is set to true*/
	private boolean my_finish;
	
	/**
	 * Constructor for the ThreadedPageRetriever.
	 * @param pages Queue of pages to be added to.
	 * @param checked Queue of valid urls to retrieve pages from.
	 */
	public ThreadedPageRetriever(BlockingQueue<PageInfo> pages, BlockingQueue<String> checked) {
		my_retrieved_count = 0;
		my_finish = false;
		my_pages = pages;
		my_url_queue = checked;
	}//end of constr
	
	/**
	 * Run method for the ThreadedPageParser thread.
	 * Ends when my_finish is set to true by outside sources
	 * or an unexpected interrupt. 
	 */
	public void run() {
		while(!my_finish) {
			try {
				doWork();
			} catch (InterruptedException e1) {
				my_finish = true;
			} catch (IOException e2) {
				System.out.println("IOException in Retriever!");
			}
		}
	}//end of run
	
	/**Ends the thread.*/
	public void finish() {
		my_finish = true;
	}//end of finish
	
	/**
	 * Gets the number of pages retrieved so far.
	 * @return Number of pages.
	 */
	public int getRetrievedCount() {
		return my_retrieved_count;
	}//end of get
	
	/**
	 * Method where the main work of the thread takes place when
	 * there are available urls to retrieve pages from.
	 * Thread is idle otherwise.
	 * @throws InterruptedException The thread was interrupted and should exit.
	 */
	private void doWork() throws InterruptedException, IOException {
		if(!my_url_queue.isEmpty()) {
			String url_string = my_url_queue.take();
			URL url = new URL(url_string);
			Source src = new Source(url);
			PageInfo p = new PageInfo(url_string, src);
			my_pages.add(p);
			my_retrieved_count++;
		}
	}//end of doWork	
}//end of class
